home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3174 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: newsjunkie.ans.net!lexis-nexis!echo!kentb
  2. From: kentb@meaddata.com (Kent Bruton)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ and lex?
  5. Date: 22 Jan 1996 17:17:20 GMT
  6. Organization: LEXIS-NEXIS, Dayton OH
  7. Message-ID: <4e0gr0$1c9@meaddata.lexis-nexis.com>
  8. References: <4dugkp$fl8@apakabar.cc.columbia.edu>
  9. NNTP-Posting-Host: echo.lexis-nexis.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Paul E Gunnells (peg14@aloha.cc.columbia.edu) wrote:
  13. > Hello everyone:
  14.  
  15. > Has anyone ever tried using lex along with iostream.h to handle input?
  16. > Or better yet, is there some equivalent to lex that works well with
  17. > C++?
  18.  
  19. > Of course, one solution is to have an extern C function yylex() that
  20. > just returns tokens that your C++ program deals with, but a better
  21. > solution (at least a cleaner solution) would be a version of lex that
  22. > allows you to directly incorporate C++ code (streams, constructors)
  23. > into the lex source.
  24.  
  25. > Is there such a beast?
  26.  
  27. I'm currently in the process of reading up on a creature called 
  28. flex 2.5.  There's a section in the man page on generating C++ 
  29. scanners.  I'm not yet to the point of having tried it but 
  30. here's a snip:
  31.  
  32. GENERATING C++ SCANNERS
  33.  
  34.      The (experimental) facility for generating C++ scanner classes.
  35.  
  36.      flex provides two different ways to  generate  scanners  for
  37.      use  with C++.  The first way is to simply compile a scanner
  38.      generated by flex using a C++ compiler instead of a  C  com-
  39.      piler.   You  should  not  encounter any compilations errors
  40.      (please report any you find to the email  address  given  in
  41.      the  Author  section  below).   You can then use C++ code in
  42.      your rule actions instead of C code.  Note that the  default
  43.      input  source  for  your  scanner  remains yyin, and default
  44.      echoing is still done to yyout. Both of these remain FILE  *
  45.      variables and not C++ streams.
  46.  
  47.      You can also use flex to generate a C++ scanner class, using
  48.      the   - +  option  (or, equivalently, %option c++), which is
  49.      automatically specified if the name of the  flex  executable
  50.      ends  in a '+', such as flex++. 
  51.  
  52.      ...
  53.  
  54.      The first class, FlexLexer, provides an abstract base  class
  55.      defining  the  general scanner class interface.
  56.  
  57.      ...
  58.  
  59.      The second class  defined  in  FlexLexer.h  is  yyFlexLexer,
  60.      which  is  derived  from FlexLexer. It defines the following
  61.      additional member functions:
  62.  
  63.      yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )
  64.           constructs a yyFlexLexer object using the given streams
  65.           for  input  and  output.  If not specified, the streams
  66.           default to cin and cout, respectively.
  67.  
  68.      ...
  69.  
  70. We'll see. 
  71.  
  72. Kent
  73.  
  74.  
  75.